﻿using System.Collections.ObjectModel;
using System.Windows.Input;
using WhiteFlow.Wpf.Common.Commands;
using WhiteFlow.DevTrackingTool.UI.Models;

namespace WhiteFlow.DevTrackingTool.UI.ViewModels
{
    /// <summary>
    /// The ViewModel that manages and manipulates a projects list.
    /// </summary>
    public class ProjectsListViewModel : WorksheetChildViewModelBase
    {
        #region Constants

        #endregion

        #region Fields

        private ICommand createProjectsListCommand;

        private ICommand addProjectCommand;

        private ICommand deleteSelectedProjectCommand;

        #endregion

        #region Commands

        /// <summary>
        /// Gets the command that creates a projects list.
        /// </summary>
        public ICommand CreateProjectsListCommand
        {
            get
            {
                if (createProjectsListCommand == null)
                {
                    createProjectsListCommand = new DelegateCommand(
                        CreateProjectsList,
                        CanCreateProjectsList);
                }

                return createProjectsListCommand;
            }
        }

        /// <summary>
        /// Gets the command that adds a projects to projects list.
        /// </summary>
        public ICommand AddProjectCommand
        {
            get
            {
                if (addProjectCommand == null)
                {
                    addProjectCommand = new DelegateCommand(
                        AddProject,
                        CanAddProject);
                }

                return addProjectCommand;
            }
        }

        /// <summary>
        /// Gets the command that deletes the currently selected project from projects list.
        /// </summary>
        public ICommand DeleteSelectedProjectCommand
        {
            get
            {
                if (deleteSelectedProjectCommand == null)
                {
                    deleteSelectedProjectCommand = new DelegateCommand<object>(
                        DeleteSelectedProject,
                        CanDeleteSelectedProject);
                }

                return deleteSelectedProjectCommand;
            }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ProjectsListViewModel"/> class.
        /// </summary>
        /// <param name="ownerWorksheetViewModel">The <see cref="WorksheetViewModel"/> instance that owns this instance.</param>
        public ProjectsListViewModel(WorksheetViewModel ownerWorksheetViewModel) : base(ownerWorksheetViewModel) { }

        #endregion

        #region Commands Implementations

        private void CreateProjectsList()
        {
            OwnerWorksheetViewModel.Worksheet.Projects = new ObservableCollection<Project>();
        }

        private bool CanCreateProjectsList()
        {
            return (OwnerWorksheetViewModel.Worksheet.Projects == null);
        }

        private void AddProject()
        {
            Project project = new Project();
            ownerWorksheetViewModel.Worksheet.Projects.Add(project);
        }

        private bool CanAddProject()
        {
            return (ownerWorksheetViewModel.Worksheet.Projects != null);
        }

        private void DeleteSelectedProject(object selectedItem)
        {
            Project project = selectedItem as Project;
            int index = OwnerWorksheetViewModel.Worksheet.Projects.IndexOf(project);

            if (index != -1)
            {
                OwnerWorksheetViewModel.Worksheet.Projects.RemoveAt(index);
            }
        }

        private bool CanDeleteSelectedProject(object selectedItem)
        {
            return (selectedItem != null);
        }

        #endregion
    }
}